﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Unsalable : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count != 0)
        {
            Button1.Visible = true;
        }
        else
        {
            Button1.Visible = false;
            GridView1_empty();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection sqlcon;
        SqlCommand sqlcom;

        String strConnection = "Server=.;Database=keshe2;Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        string sqlstr = "Delete from 选购 where 商品编号 in(SELECT 商品编号 FROM 商品 WHERE (商品编号 NOT IN (SELECT 商品编号 FROM 订单)))";
        sqlcon = new SqlConnection(strConnection);
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();

        sqlstr = "Delete from 商品 where 商品编号 in(SELECT 商品编号 FROM 商品 WHERE (商品编号 NOT IN (SELECT 商品编号 FROM 订单)))";
        sqlcon = new SqlConnection(strConnection);
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        con.Close();
        Button1.Visible = false;
       GridView1_empty();
    }
    //ShowHeaderWhenEmpty="True"
    protected void GridView1_empty()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("商品编号");
        dt.Columns.Add("商品名称");
        dt.Columns.Add("数量");
        dt.Columns.Add("价格");
        dt.Columns.Add("名称");
        if (dt.Rows.Count == 0)
        {
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSourceID = "";
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int columnCount = dt.Columns.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            GridView1.Rows[0].Cells[0].Text = "暂无滞销商品记录！";
            GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
        }
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}